package com.wellsoft.mobileoa.utils.widget;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.app.AlertDialog;
import android.view.View;

import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.BaseWidget;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageField;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageFieldButton;
import com.wellsoft.mobileoa.wsmodel.WSOA_PostField;
import com.wmobile.utils.ServiceResponse;

public class HiddenWidget extends BaseWidget {

	public HiddenWidget(WSOA_PageField field, BaseView baseView) {
		this.field = field;
		this.baseView = baseView;
	}

	@Override
	public View getView() {
		return null;
	}

	@Override
	public void setValue(Object widgetValue) {
		String oldValue = field.getFieldValue();
		String newValue = widgetValue.toString();
		if (SystemConfig.stringIsNullOrEmpty(oldValue)
				&& SystemConfig.stringIsNullOrEmpty(newValue)) {
			return;
		}
		if (!SystemConfig.stringIsNullOrEmpty(oldValue)
				&& oldValue.toUpperCase().equals("FULL")
				&& SystemConfig.stringIsNullOrEmpty(newValue)) {
			setHasEdited(true);
			return;
		}
		if (!newValue.equals(oldValue)) {
			field.setFieldValue(widgetValue.toString());
			baseView.setHasEdited(true);
			setHasEdited(true);
			setHasValided(false);
		}
	}

	@Override
	public WSOA_PostField getValue() {
		WSOA_PostField postField = new WSOA_PostField();
		postField.setFieldName(field.getFieldName());
		List<String> values = new ArrayList<String>();
		if (!SystemConfig.stringIsNullOrEmpty(field.getFieldValue())) {
			String tmpSeparator = field.getSeparator();
			if (!SystemConfig.stringIsNullOrEmpty(tmpSeparator)) {
				if (tmpSeparator.toUpperCase().equals("LINE")) {
					tmpSeparator = "\n";
				}
				String[] hiddenValueStrings = field.getFieldValue().split(
						tmpSeparator);
				for (int i = 0; i < hiddenValueStrings.length; i++) {
					values.add(hiddenValueStrings[i]);
				}
			} else {
				values.add(field.getFieldValue());
			}
		}
		postField.setFieldValues(values);
		return postField;
	}

	@Override
	public String getRawValue() {
		if (SystemConfig.stringIsNullOrEmpty(field.getFieldValue())) {
			return "";
		} else {
			return field.getFieldValue();
		}
	}

	@Override
	public ServiceResponse checkValid() {
		if (hasValided) {
			return new ServiceResponse(true);
		}
		if (SystemConfig.stringIsNullOrEmpty(field.getFieldValue())
				&& field.getFieldButtons() != null
				&& field.getFieldButtons().size() > 0) {
			AlertDialog alertDialog = baseView.getBaseActivity()
					.showWaitingDialog();
			WSOA_PageFieldButton button = field.getFieldButtons().get(0);
			ServiceResponse response = BaseAction.executeActions(
					button.getActionID(), button.getAction(), baseView);
			alertDialog.dismiss();
			if (!response.getExeFlag()) {
				return response;
			}
		}

		if (field.getRequired() != null) {
			if (field.getRequired().equals("1")
					&& SystemConfig.stringIsNullOrEmpty(field.getFieldValue())) {
				setHasValided(false);
				return new ServiceResponse(false, field.getFieldTitle()
						+ "不能为空！");
			} else {
				setHasValided(true);
				return new ServiceResponse(true);
			}
		}
		return new ServiceResponse(true);
	}

}
